package com.example.firstapp;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.firstapp.entity.Phone;

import service.PhoneService;
import util.MyDatabaseHelper;

public class Test05_2Activity extends AppCompatActivity {
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test05_2);


        //链接数据库
        MyDatabaseHelper helper = new MyDatabaseHelper(this, "FistApp.db",
                null, 1);

        //获取数据库对象
        db = helper.getWritableDatabase();

        //绑定组件
        EditText et_name = findViewById(R.id.et_name1);
        EditText et_phone = findViewById(R.id.et_photo1);
        Button bt_phone_add = findViewById(R.id.bt_photo_add1);
        Button bt_phone_delete = findViewById(R.id.bt_photo_delete1);
        Button bt_phone_update = findViewById(R.id.bt_photo_update1);

        //init
        Intent intent = getIntent();
        if (intent.getStringExtra("type").equals("0")) {
            bt_phone_delete.setVisibility(View.INVISIBLE);
            bt_phone_update.setVisibility(View.INVISIBLE);
            bt_phone_add.setVisibility(View.VISIBLE);
        } else {
            bt_phone_delete.setVisibility(View.VISIBLE);
            bt_phone_update.setVisibility(View.VISIBLE);
            bt_phone_add.setVisibility(View.INVISIBLE);
            et_name.setText(intent.getStringExtra("name"));
            et_phone.setText(intent.getStringExtra("phone"));
        }

        //添加联系人
        bt_phone_add.setOnClickListener(view -> {
            Phone phone = new Phone(et_name.getText().toString(), et_phone.getText().toString());
            PhoneService.phone_add(db, phone);
            message(phone.getMessage());
            if (phone.isState()) {
                Intent intent01 = new Intent(this, Test05_1Activity.class);
                startActivity(intent01);
                Test05_2Activity.this.finish();
            }
        });

        //删除按键绑定
        bt_phone_delete.setOnClickListener(view -> {
            Phone phone = new Phone(et_name.getText().toString());
            PhoneService.phone_delete(db, phone);
            message(phone.getMessage());
            if (phone.isState()) {
                Intent intent01 = new Intent(this, Test05_1Activity.class);
                startActivity(intent01);
                Test05_2Activity.this.finish();
            }
        });

        //修改按键绑定
        bt_phone_update.setOnClickListener(view -> {
            Phone phone = new Phone(et_name.getText().toString(), et_phone.getText().toString());
            PhoneService.phone_update(db, phone);
            message(phone.getMessage());
            if (phone.isState()) {
                Intent intent01 = new Intent(this, Test05_1Activity.class);
                startActivity(intent01);
                Test05_2Activity.this.finish();
            }
        });

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Intent intent01 = new Intent(this, Test05_1Activity.class);
        startActivity(intent01);
        Test05_2Activity.this.finish();
    }

    private void message(String message) {
        Toast.makeText(Test05_2Activity.this, message, Toast.LENGTH_LONG).show();
    }
}